package com.tinyblog.ajax;

import java.io.IOException;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.text.SimpleDateFormat;

public class MessageAction extends HttpServlet {
	private static final long serialVersionUID = -2888338190137446250L;
	/**
	 * ���������
	 */
	public void service (HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException {
		String action = "";
		if(request.getParameter("action")!=null) action = request.getParameter("action");
		System.out.println("�������ͣ�"+action);
		if("addMessage".equals(action))
			this.addMessage(request, response);
		else if("updateMessage".equals(action))
			this.updateMessage(request, response);
		else if("deleteMessage".equals(action))
			this.deleteMessage(request, response);
		else if("viewMessage".equals(action))
			this.viewMessage(request, response);
		else if("findMessage".equals(action))
			this.findMessage(request, response);
		else if("viewSingleMessage".equals(action))
			this.viewSingleMessage(request, response);
	}
	/**
	 * �������
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	private void addMessage(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		AjaxMessage ajaxMessage = new AjaxMessage();
		if(request.getParameter("messagetitle")!=null)
			ajaxMessage.setTitle(ExtendString.decodeToUtf8(request.getParameter("messagetitle")));
		if(request.getParameter("author")!=null)
			ajaxMessage.setAuthor(ExtendString.decodeToUtf8(request.getParameter("author")));
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		ajaxMessage.setSubmittime(sdf.format(new Date()));
		if(request.getParameter("qq")!=null)
			ajaxMessage.setQq(ExtendString.decodeToUtf8(request.getParameter("qq")));
		if(request.getParameter("msn")!=null)
			ajaxMessage.setMsn(ExtendString.decodeToUtf8(request.getParameter("msn")));
		if(request.getParameter("yahoo")!=null)
			ajaxMessage.setYahoo(ExtendString.decodeToUtf8(request.getParameter("yahoo")));
		if(request.getParameter("email")!=null)
			ajaxMessage.setEmail(ExtendString.decodeToUtf8(request.getParameter("email")));
		if(request.getParameter("content")!=null)
			ajaxMessage.setContent(ExtendString.decodeToUtf8(request.getParameter("content")));
		AjaxMessageService service = new AjaxMessageService();
		try {
			service.addMessage(ajaxMessage);
		}catch(Exception ex) {
			this.handleError(ex);
		}
		this.forward("../message/messageAction?action=viewMessage",request,response);
	}
	/**
	 * �޸�����
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void updateMessage(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		AjaxMessageService service = new AjaxMessageService();
		if(request.getParameter("id")!=null) {
			AjaxMessage ajaxMessage = service.getMessage(request.getParameter("id"));
			if(request.getParameter("messagetitle")!=null)
				ajaxMessage.setTitle(ExtendString.decodeToUtf8(request.getParameter("messagetitle")));
			if(request.getParameter("author")!=null)
				ajaxMessage.setAuthor(ExtendString.decodeToUtf8(request.getParameter("author")));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			ajaxMessage.setSubmittime(sdf.format(new Date()));
			if(request.getParameter("qq")!=null)
				ajaxMessage.setQq(ExtendString.decodeToUtf8(request.getParameter("qq")));
			if(request.getParameter("msn")!=null)
				ajaxMessage.setMsn(ExtendString.decodeToUtf8(request.getParameter("msn")));
			if(request.getParameter("yahoo")!=null)
				ajaxMessage.setYahoo(ExtendString.decodeToUtf8(request.getParameter("yahoo")));
			if(request.getParameter("email")!=null)
				ajaxMessage.setEmail(ExtendString.decodeToUtf8(request.getParameter("email")));
			if(request.getParameter("content")!=null)
				ajaxMessage.setContent(ExtendString.decodeToUtf8(request.getParameter("content")));
			try {
				service.updateMessage(ajaxMessage);
			}catch(Exception ex) {
				this.handleError(ex);
			}
		}
		else {
			this.handleError(new Exception("δָ�����Ա�š�"));
		}
		this.forward("../message/messageAction?action=viewMessage",request,response);
	}
	/**
	 * ɾ��ָ��������
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void deleteMessage(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		AjaxMessageService service = new AjaxMessageService();
		if(request.getParameter("id")!=null) {
			try {
				service.deleteMessage(request.getParameter("id"));
			}catch(Exception ex) {
				this.handleError(ex);
			}
		}else {
			this.handleError(new Exception("δָ�����Ա�š�"));
		}
		this.forward("../message/messageAction?action=viewMessage",request,response);
	}
	/**
	 * �鿴����
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void viewMessage(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		AjaxMessageService service = new AjaxMessageService();
		try {
			response.setContentType("application/xml");
			service.output(response.getWriter(),null);
		}catch(Exception ex) {
			this.handleError(ex);
		}
	}
	/**
	 * �鿴����������
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void viewSingleMessage(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		AjaxMessageService service = new AjaxMessageService();
		String id = request.getParameter("id");
		try {
			AjaxMessage ajaxMessage = service.getMessage(id);
			response.setContentType("application/xml");
			service.outputSingleMessage(response.getWriter(), ajaxMessage);
		}catch(Exception ex) {
			this.handleError(ex);
		}
	}
	/**
	 * ��������
	 * @param request
	 * @param response
	 * @throws IOException
	 * @throws ServletException
	 */
	public void findMessage(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		MessageCriteria criteria = new MessageCriteria();
		if(request.getParameter("criteria_title")!=null)
			criteria.setTitle(ExtendString.decodeToUtf8(request.getParameter("criteria_title")));
		if(request.getParameter("criteria_author")!=null)
			criteria.setAuthor(ExtendString.decodeToUtf8(request.getParameter("criteria_author")));
		if(request.getParameter("beginSubmittime")!=null)
			criteria.setBeginSubmittime(ExtendString.decodeToUtf8(request.getParameter("beginSubmittime")));
		if(request.getParameter("endSubmittime")!=null)
			criteria.setEndSubmittime(ExtendString.decodeToUtf8(request.getParameter("endSubmittime")));
		if(request.getParameter("criteria_content")!=null)
			criteria.setContent(ExtendString.decodeToUtf8(request.getParameter("criteria_content")));
		AjaxMessageService service = new AjaxMessageService();
		try {
			AjaxMessage[] messages = service.findMessage(criteria);
			response.setContentType("application/xml");
			service.output(response.getWriter(),messages);
		}catch(Exception ex) {
			this.handleError(ex);
		}
	}
	public void handleError(Exception ex) {
		System.out.println(ex.toString());
	}
    /**
     * ����ҳ��ת��
     * @param strForward
     * @param request
     * @param response
     * @throws IOException
     * @throws ServletException
     */
    private void forward(String strForward, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
		RequestDispatcher dispatcher = request.getRequestDispatcher(strForward);
		dispatcher.forward((ServletRequest)request, (ServletResponse)response);
	}

}
